using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Library.Components.Lang;
using Framework.Library.Components.Commands;

namespace Framework.Portal.Administrator.Portals
{
    public partial class PortalManagement : AuthenticatedControl
    {
        private bool Edit = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {                
                string srtEdit = Request.QueryString["edit"];
                if (!String.IsNullOrEmpty(srtEdit))
                {
                    LoadData();
                }
                lblCommandName.Text = AppEnv.CommandName();
            }
        }
        private void LoadData()
        {
            int intPortalId = Convert.ToInt32(AppEnv.PortalId());
            Portal_PortalsInfo portal = PortalController.GetPortalsById(intPortalId);
            txtPortalID.Text = AppEnv.PortalId();
            chkEnable.Checked = portal.Enable;
            chkVisible.Checked = portal.Visible;
            FilePicker1.Text = portal.ImageUrl;
            dropPortal.SelectedValue = portal.Portal_ParentId.ToString();
            txtName.Text = portal.Name;
            txtDescription.Text = portal.Description;
            drgLanguage.SelectedValue = portal.Language.ToString();
            drgPriority.SelectedValue = portal.Priority.ToString();
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            dropPortal.Items.Clear();
            PortalController.LoadPortals(dropPortal.Items);
            
            dropPortal.Items.Insert(0, new ListItem("Root", "0"));
            dropPortal.SelectedValue = "0";

            drgLanguage.Items.Clear();
            MainLanguagesController.LoadLanguage(drgLanguage.Items);
            MiscUtility.FillIndex(drgPriority, 30, 1);

            DataTable dtPortals = PortalController.GetPortals();
            
             DataTable dtRebuildData = new DataTable();

            dtRebuildData.Columns.Add("PortalID", typeof(String));
            dtRebuildData.Columns.Add("Name", typeof(String));
            dtRebuildData.Columns.Add("Description", typeof(String));
            dtRebuildData.Columns.Add("Portal_ParentId", typeof(String));
            
            dgrPortals.DataSource = PortalController.GetPortalByParentID(dtPortals,dtRebuildData);
            dgrPortals.ItemDataBound +=new RepeaterItemEventHandler(dgrPortals_ItemDataBound);
            dgrPortals.DataBind();
        }

        protected void dgrPortals_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int portalId;
            switch (e.CommandName)
            {
                case "editportal":
                    portalId = ConvertUtility.ToInt32(e.CommandArgument);
                    Portal_PortalsInfo portal = PortalController.GetPortalsById(portalId);
                    txtPortalID.Text = portalId.ToString();
                    chkEnable.Checked = portal.Enable;
                    chkVisible.Checked = portal.Visible;
                    FilePicker1.Text = portal.ImageUrl;
                    dropPortal.SelectedValue = portal.Portal_ParentId.ToString();
                    txtName.Text = portal.Name;
                    txtDescription.Text = portal.Description;
                    drgLanguage.SelectedValue = portal.Language.ToString();
                    drgPriority.SelectedValue = portal.Priority.ToString();
                    Edit = true;
                    break;
                case "deleteportal":
                    try
                    {
                        portalId = ConvertUtility.ToInt32(e.CommandArgument);
                        PortalController.DeletePortal(portalId);
                        this.lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    break;               
                default:
                    break;
            }
        }
        protected void dgrPortals_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                ImageButton btnDeleteRole = (ImageButton)e.Item.FindControl("btnDeletePortal");
                btnDeleteRole.OnClientClick = MiscUtility.MSG_DELETE_CONFIRM;                
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {                
                Portal_PortalsInfo _portalInfo = new Portal_PortalsInfo();
                _portalInfo.PortalID = ConvertUtility.ToInt32(txtPortalID.Text);
                _portalInfo.Portal_ParentId = ConvertUtility.ToInt32(dropPortal.SelectedValue);
                _portalInfo.Name = txtName.Text;
                _portalInfo.Description = txtDescription.Text;
                _portalInfo.Visible = chkVisible.Checked;
                _portalInfo.Enable = chkEnable.Enabled;
                _portalInfo.Language = ConvertUtility.ToInt32(drgLanguage.SelectedValue);
                _portalInfo.ImageUrl = FilePicker1.Text;
                _portalInfo.Priority = ConvertUtility.ToInt32(drgPriority.SelectedValue);
                PortalController.UpdatePortal(_portalInfo);
                this.lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "")
            {
                try
                {
                    Portal_PortalsInfo portalInfo = new Portal_PortalsInfo();
                    portalInfo.Portal_ParentId = ConvertUtility.ToInt32(dropPortal.SelectedValue);
                    portalInfo.Name = txtName.Text;
                    portalInfo.Description = txtDescription.Text;
                    portalInfo.Visible = chkVisible.Checked;
                    portalInfo.Enable = chkEnable.Checked;
                    portalInfo.Language = ConvertUtility.ToInt32(drgLanguage.SelectedValue);
                    portalInfo.ImageUrl = FilePicker1.Text;
                    portalInfo.Priority = ConvertUtility.ToInt32(drgPriority.SelectedValue);
                    PortalController.AddPortal(portalInfo);
                    this.lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
        }        

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Edit || !String.IsNullOrEmpty(Request.QueryString["edit"]))
            {
                LoadData();
                Edit = false;
            }            
            else
            {
                dropPortal.SelectedIndex = -1;
                txtName.Text = "";
                txtDescription.Text = "";
                chkEnable.Checked = false;
                chkVisible.Checked = false;
                drgLanguage.SelectedIndex = -1;
                FilePicker1.Text = "";
                drgPriority.SelectedIndex = -1;
            }
        }
    }
}